home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / 13h_kit / keyboard.cpp < prev    next >
C/C++ Source or Header  |  1991-07-04  |  6KB  |  178 lines

  1. // KEYBOARD.CPP ************************************************************
  2. // module for low level filtered and unfiltered keyboard i/o.              *
  3. // Copyright 1991 by the Gamers Programming Workshop, a function of the    *
  4. // GAMERS forum, Compuserve. For more info e-mail 76605,2346.              *
  5. //                                                                         *
  6. // License is granted for use or modification of this code as long as      *
  7. // this notice remains intact, and all improvements are listed in the      *
  8. // version history below, and uploaded to the GAMERS forum. This code      *
  9. // may not be used for any commercial purpose.                             *
  10. //                                                                         *
  11. //**************************************************************************
  12.  
  13. //**************************************************************************
  14. // Version history:                                                        *
  15. //                                                                         *
  16. // Version 1.0                                                             *
  17. // Developed: May 30, 1991                                                 *
  18. // Author:    Mark Betz, 76605, 2346                                       *
  19. // Last update: July 5, 1991                                               *
  20. //**************************************************************************
  21.  
  22. #include <stdio.h>
  23. #include <conio.h>
  24. #include "keyboard.hpp"
  25.  
  26. // ************************************************************************
  27. // getkey() is passed pointers to type char, for the ascii code, and type
  28. // extnd, for the scan code. If either is NULL the funtion ignores it. The
  29. // function waits if no key has been pressed, so test with kbhit() if you
  30. // don't want to hang here.
  31. // ************************************************************************
  32.  
  33. void getkey(char *key, extnd *scan) {
  34.     asm {
  35.         mov ah, 0x10
  36.         int 0x16
  37.     }
  38.     if (key!=NULL)
  39.         *key=_AL;
  40.     if (scan!=NULL) {
  41.         if (*key==0)
  42.             *scan=(extnd)_AH;
  43.         else
  44.             *scan=NO_EXT;
  45.     }
  46. }
  47.  
  48. // ************************************************************************
  49. // getfilteredkey() compares the next keycode combination retrieved from
  50. // the buffer based on the filter in mask. If a key matches the function
  51. // stores the key data in key and scan, and returns a boolean true. If no
  52. // match exists for the key, the function returns false, and the values
  53. // of scan and key are unchanged. NULL pointer arguments are not allowed.
  54. // ************************************************************************
  55.  
  56. boolean getfilteredkey(char mask, char *key, extnd *scan) {
  57.     char main,aux;
  58.     asm {
  59.         mov ah, 0x10
  60.         int 0x16
  61.     }
  62.     main=_AL;
  63.     aux =_AH;
  64.     if (!((mask&UCASE)&&                  // filter for uppercase chars
  65.        (main>64) && (main<91))) {
  66.  
  67.      if (!((mask&LCASE)&&                 // filter for lower case chars
  68.         (main>96) && (main<123))) {
  69.  
  70.       if (!((mask&BCASE)&&                // filter for chars of both cases
  71.          (((main>64) && (main<91))||
  72.          ((main>96) && (main<123))))) {
  73.  
  74.        if (!((mask&NUMBER)&&               // filter for numeric chars
  75.           (main>47) && (main<58))) {
  76.  
  77.         if (!((mask&FUNCT)&&               // filter for keys f1-f10
  78.            (main==0)&&
  79.            ((aux>58) && (aux<69)) ||
  80.            ((aux>132) && (aux<135)))) {
  81.  
  82.          if (!((mask&FUNCT)&&              // filter for shift f1- shift f10
  83.             (main==0)&&
  84.             (((aux>83) && (aux<94))||
  85.             ((aux>134) && (aux<137))))) {
  86.  
  87.           if (!((mask&FUNCT)&&             // filter for ctrl f1- ctrl f10
  88.              (main==0)&&
  89.              (((aux>93) && (aux<104))||
  90.              ((aux>136) && (aux<139))))) {
  91.  
  92.            if (!((mask&FUNCT)&&            // filter for alt f1- alt f10
  93.               (main==0)&&
  94.               (((aux>103) && (aux<114))||
  95.               ((aux>138) && (aux<141))))) {
  96.  
  97.             if (!((mask&CURSOR)&&          // filter for cursor command keys
  98.                ((main==0)||(main==224))&&  // source keypad or standalone
  99.                (((aux>69) && (aux<74))||   // home, up arrow, pageup
  100.                (aux==75)||(aux==77)||      // left arrow, right arrow
  101.                ((aux>78) && (aux<84))||    // end, dn arrw, pgdwn, ins, del
  102.                ((aux>113) && (aux<120))||  // ctrl-cursor combos
  103.                (aux==132)))) {
  104.  
  105.              if (!((mask&PUNCT)&&          // filter for punctuation marks
  106.                 ((main==33)||(main==34)||  // ! and "
  107.                 (main==39)||(main==44)||   // ' and ,
  108.                 (main==46)||(main==96)||   // . and `
  109.                 (main==13)||(main==8)||    // enter and backspace
  110.                 (main==32)||(main==9)))) { // space and tab
  111.  
  112.               if (!((mask&ESC)&&(main==27))) {
  113.  
  114.                     return(false);
  115.               }
  116.              }
  117.             }
  118.            }
  119.           }
  120.          }
  121.         }
  122.        }
  123.       }
  124.      }
  125.     }
  126.     *key=main;
  127.     *scan=(extnd)aux;
  128.     return(true);
  129. }
  130.  
  131. // ************************************************************************
  132. // stuffbuffer() places the passed key and scan codes in the keyboard
  133. // buffer. stuffbuffer() does not check for a full keyboard buffer. It is
  134. // recommended that flushbuffer() be called prior to using this service,
  135. // and that this function be called a maximum of 15 times between keyboard
  136. // reads.
  137. // ************************************************************************
  138.  
  139. void stuffbuffer(char key, extnd scan) {
  140.     char s;
  141.     s=scan;
  142.     asm {
  143.         mov ah, 5
  144.         mov cl, key
  145.         mov ch, s
  146.         int 0x16
  147.     }
  148. }
  149.  
  150. // ************************************************************************
  151. // flushbuffer() flushes the keyboard buffer. If buf != NULL it flushes the
  152. // key and scan codes to the memory buffer pointed to by buf, and returns
  153. // the number of characters read. If buf=NULL, or no characters are waiting
  154. // it returns 0.
  155. // ************************************************************************
  156.  
  157. int flushbuffer(char *buf) {
  158.     char c;
  159.     extnd s;
  160.     int i;
  161.  
  162.     if (buf!=NULL) {
  163.         for (i=0;kbhit();) {
  164.             getkey(&c,&s);
  165.             buf[i++]=c;
  166.             buf[i++]=s;
  167.         }
  168.         return(i/2);
  169.     } else
  170.         while (kbhit()) {
  171.             getkey(&c,&s);
  172.         }
  173.     return(0);
  174. }
  175.  
  176.  
  177.  
  178.